package dao.Impl;

import dao.PetDao;
import entity.Pet;
import handler.CustomBeanPetListHandler;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.C3p0Helper;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PetDaoImpl implements PetDao {
    @Override
    public List<Pet> findAll() {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from pet";
        try {
            return qr.query(sql,new CustomBeanPetListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Pet> findPetByName(String petName) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from pet where petName like ?";
        try {
            return qr.query(sql, new BeanListHandler<>(Pet.class), "%"+petName+"%");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Pet findPetById(int id) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from pet where id=?";
        try {
            return (Pet) qr.query(sql, new BeanListHandler<>(Pet.class), id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void addPet(Pet pet) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "insert into pet(petName,petType,sex,birthday,pic,state,remark) values(?,?,?,?,?,?,?)";
        try {
            qr.update(sql,pet.getPetName(),pet.getPetType(),pet.getSex(),pet.getBirthday(),pet.getPic(),pet.getState(),pet.getRemark());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void updatePet(Pet pet) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "update pet set petName=?, petType=?, sex=?, birthday=?, pic=?, state=?, remark=? where id=?";
        try {
            // 确保传递所有8个参数
            qr.update(sql, pet.getPetName(), pet.getPetType(), pet.getSex(), pet.getBirthday(), pet.getPic(), pet.getState(), pet.getRemark(), pet.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deletePet(int id) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "delete from pet where id=?";
        try {
            qr.update(sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Pet> findNoPet() {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from pet where state =0 or state =1";
        try {
            return qr.query(sql,new CustomBeanPetListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Pet> findPetByNoState() {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from pet where petType=? and state=0 or state=1";
        try {
            return qr.query(sql,new CustomBeanPetListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
